package dk.fuddi.grails

class ToiletService {

  static transactional = true

  String url = "http://beta.findtoilet.dk/xml/term/2" //København

  def getToilets() {
    log.info "Get toilets from url [$url]"
    def text = url.toURL().text
    def xml = new XmlSlurper().parseText(text)
    List<Toilet> toiletList = new ArrayList<Toilet>()
    xml.node.each {
      Toilet toilet = new Toilet()
      toilet.nid = it.nid.toLong()
      toilet.street = it.street
      toilet.zipCode = it.postalcode.toInteger()
      toilet.city = it.city
      toilet.lat = it.latitude.toDouble()
      toilet.lng = it.longitude.toDouble()
      toilet.type = it.type
      toiletList.add(toilet)
    }
    return toiletList
  }
}
